In [1]:
import sys
import os
sys.path.append(os.path.abspath("/home/scidb/HeartRatePatterns/Python"))
from LogisticRegresion import ajustLogisticRegression
from Matrix import convert_matrix
from sklearn.model_selection import train_test_split
In [2]:
table = convert_matrix(with_pearson=252,len_words=(3,3))
survived = table.index.labels[1].tolist()
patients = table.values
patients_train, patients_test,survived_train, survived_test = train_test_split(patients,
survived,test_size=0.2, random_state=42)
In [3]:
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(patients_train, survived_train)
Out[3]:
In [4]:
ajustedmodel = ajustLogisticRegression(patients_train,survived_train,patients_test,survived_test)['model']
In [5]:
from sklearn.metrics import accuracy_score
acurracy = accuracy_score(survived_test, model.predict(patients_test))
acurracyAjust = accuracy_score(survived_test, ajustedmodel.predict(patients_test))
print("normal",acurracy,"ajusted",acurracyAjust)
In [6]:
from sklearn.metrics import roc_auc_score
print("normal")
probaNormal = model.predict_proba(patients_test)[:, 1]
roc_auc_normal = roc_auc_score(survived_test, probaNormal)
print(roc_auc_normal)
print("ajusted")
probaAjusted = ajustedmodel.predict_proba(patients_test)[:, 1]
roc_auc_ajusted = roc_auc_score(survived_test, probaAjusted)
print(roc_auc_ajusted)
In [7]:
from sklearn.metrics import confusion_matrix
print("normal")
confusion_normal = confusion_matrix(survived_test,model.predict(patients_test))
print(confusion_normal)
print("ajusted")
confusion_ajusted = confusion_matrix(survived_test,ajustedmodel.predict(patients_test))
print(confusion_ajusted)
In [8]:
from sklearn.metrics import roc_curve
import matplotlib.pyplot as plt
In [18]:
def roc_curveplot(name,y_true,y_score,logit_roc_auc,acurracy):
fpr, tpr, thresholds = roc_curve(y_true,y_score)
plt.plot(fpr,tpr,label=str(name)+' AUC =%.2f' % logit_roc_auc+' acc ={:.2f}%'.format(acurracy*100) )
In [20]:
fig_size = [6, 6]
plt.rcParams["figure.figsize"] = fig_size
plt.figure()
roc_curveplot('Normal',survived_test,probaNormal,roc_auc_normal,acurracy)
roc_curveplot('Ajustado',survived_test,probaAjusted,roc_auc_ajusted,acurracyAjust)
plt.plot([0,1],[0,1],'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Tasa de Falsos Positivos')
plt.ylabel('Tasa de Verdaderos Positivos')
plt.title('Curva ROC comparando resultados')
plt.legend(loc="lower right")
plt.show()
In [ ]: